@extends('admin.layout.base')
@section('styles')
    <style>
        .ad-img {
            width: 210px;
            height: 140px;
            border: 1px dashed #d9d9d9;
            display: block;
        }
        .ad-uploader-icon {
            cursor: pointer;
            font-size: 28px;
            color: #8c939d;
            width: 210px;
            height: 140px;
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            line-height: 150px;
            text-align: center;
        }
    </style>
@endsection
@section('content')
    <div class="panel-content" id="app" v-cloak>
        <div class="panel-body panel-info">
            <el-form :model="form" ref="form" class="form" label-width="100px">
                <div class="panel-header"> <div class="panel-title"> 基本信息 </div> </div>
                <div class="panel-main">
                    <el-form-item :rules="[{ required: true, message: '请填写标题', trigger: 'blur'}]" label="标题：" prop="title">
                        <el-input clearable v-model="form.title" placeholder="请填写标题"></el-input>
                    </el-form-item>
                    <el-form-item :rules="[{ required: true, message: '请上传封面图', trigger: 'blur'}]" label="封面图：" prop="thumb">
                        <el-upload
                                class="ad-uploader"
                                :action="upload_image"
                                :show-file-list="false"
                                name="image"
                                :limit="1"
                                :on-success="uploadSuccess"
                                :before-upload="uploadBefore" ref="upload">
                            <img v-if="form.thumb" :src="form.thumb" class="ad-img">
                            <i v-else class="el-icon-plus ad-uploader-icon"></i>
                            <div slot="tip" class="el-upload__tip">图片大小不能超过 2MB，建议尺寸3:2!</div>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="内容：" :rules="[{ required: true, message: '请填写内容', trigger: 'blur'}]" prop="content">
                        <div id="editor"></div>
                    </el-form-item>
                </div>
                <div class="panel-header"> <div class="panel-title"> 其他信息 </div> </div>
                <div class="panel-main">
                    <el-form-item label="浏览量：" prop="view">
                        <el-input clearable v-model="form.view" placeholder="请填写浏览量"></el-input>
                    </el-form-item>
                    <el-form-item label="收藏量：" prop="collect">
                        <el-input clearable v-model="form.collect" placeholder="请填写收藏量"></el-input>
                    </el-form-item>
                    <el-form-item label="来源：" prop="source">
                        <el-input clearable v-model="form.source" placeholder="请填写信息来源"></el-input>
                    </el-form-item>
                    <el-form-item required label="热门：" required prop="hot">
                        <el-radio-group v-model="form.hot">
                            <el-radio :label="1" border>正常</el-radio>
                            <el-radio :label="2" border>热门</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item required label="状态：" required prop="status">
                        <el-radio-group v-model="form.status">
                            <el-radio :label="1" border>正常</el-radio>
                            <el-radio :label="2" border>锁定</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </div>
                <el-form-item style="padding-left: 150px;margin-top: 50px">
                    <el-button type="primary" @click="update()">保 存</el-button>
                    <el-button @click="back()" style="margin-left: 50px">返 回</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
@endsection
@section('scripts')
    <script src="{{ asset('neditor/neditor.config.js') }}" type="text/javascript"></script>
    <script src="{{ asset('neditor/neditor.all.min.js') }}" type="text/javascript"></script>
    <script src="{{ asset('neditor/neditor.service.js') }}" type="text/javascript"></script>
    <script src="{{ asset('neditor/i18n/zh-cn/zh-cn.js') }}" type="text/javascript"></script>
    <script src="{{ asset('neditor/third-party/browser-md5-file.min.js') }}" type="text/javascript"></script>
    <script src="{{ asset('neditor/third-party/jquery-1.10.2.min.js') }}" type="text/javascript"></script>
    <script>
        $(function () {
            let app = new Vue({
                el: '#app',
                data: function () {
                    return {
                        info: {!! $info !!},
                        upload_image: UPLOAD_IMAGE,
                        form: {
                            id: " {{ $id }}",
                            title: '',
                            content: '',
                            thumb: '',
                            status: 1,
                            hot: 1,
                            view: 0,
                            collect: 0,
                            source: '',
                        },
                        editor: null,
                    }
                },
                created() {
                    this.editor = UE.getEditor('editor');
                    let that = this;
                    if (this.form.id > 0) {
                        this.form = {
                            id: this.info.id,
                            title: this.info.title,
                            content: this.info.content,
                            thumb: this.info.thumb,
                            status: this.info.status,
                            hot: this.info.hot,
                            view: this.info.view,
                            collect: this.info.collect,
                            source: this.info.source,
                        };
                        this.editor.ready(function () {
                            that.editor.setContent(that.form.content);
                        });
                    }
                    this.editor.ready(function () {
                        that.editor.setPlaceholder('请填写内容');
                    });
                },
                methods: {
                    uploadSuccess(res) {
                        this.form.thumb = res.data.path;
                        this.$refs.upload.clearFiles(); //上传成功之后清除历史记录
                    },
                    uploadBefore(file) {
                        const isLt2M = file.size / 1024 / 1024 < 2;
                        if (!isLt2M) {
                            this.$message.error('图片大小不能超过 2MB!');
                        }
                        return isLt2M;
                    },
                    //返回
                    back() {
                        window.history.go(-1);
                    },
                    //更新
                    update() {
                        this.form.content = this.editor.getContent();
                        this.form._token = "{{csrf_token()}}";
                        this.$refs['form'].validate((valid) => {
                            if (!this.form.content) {
                                this.$refs.form.fields[3].error = "请填写内容";
                                return false;
                            }
                            if (valid) {
                                let that = this;
                                $.post("{{ route('admin.article.update') }}", this.form, function (res) {
                                    if (res.code) {
                                        that.$message.error(res.msg);
                                    } else {
                                        that.$message({
                                            type: 'success',
                                            duration: '1000',
                                            message: res.msg,
                                            onClose: () => {
                                                window.history.go(-1);
                                            }
                                        });
                                    }
                                }, 'JSON');
                            }
                        });
                    },
                }
            });
        })
    </script>
@endsection
